package cn.chinaflame.pagodas.web;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.annotation.ApiDoc;
import cn.chinaflame.pagodas.dto.PaProductDto;
import cn.chinaflame.pagodas.req.ProductQryReq;
import cn.chinaflame.pagodas.req.ProductReq;
import cn.chinaflame.pagodas.service.CommonService;
import cn.chinaflame.pagodas.service.PaProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.entity.Boy;
import cn.chinaflame.pagodas.repository.BoyRepository;
import cn.chinaflame.pagodas.service.BoyService;
import cn.chinaflame.pagodas.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

/**
 * 商品相关接口
 *
 * @author admin
 */
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "openapi/v1/pagodas/product/")
public class OpenPaProductAPI {
    private static Logger log = LoggerFactory.getLogger(OpenPaProductAPI.class);
    @Autowired
    private PaProductService productService;
    @Autowired
    private CommonService commonService;

    /**
     * 商品添加
     *
     * @param data={"name":"name","itemId":"itemId"}
     * @return
     */
    @PostMapping(value = "add")
    @ApiDoc(ProductReq.class)
    public Object add(@RequestBody ProductReq req) {
        log.info("商品添加接口data:{}", req);
        req.check();
        PaProductDto rs = productService.add1(req);
        return Result.create(rs);
    }

    /**
     * 商品修改
     *
     * @param data={"id":"id","name":"name","itemId":"itemId"}
     * @return
     */
    @PutMapping(value = "update")
    @ApiDoc(ProductReq.class)
    public Object update(@RequestBody ProductReq req) {
        log.info("商品修改接口req:{}", req);
        req.check();
        PaProductDto rs = productService.update(req);
        return Result.create(rs);
    }

    /**
     * 商品删除
     *
     * @param id 商品id
     * @return
     */
    @DeleteMapping(value = "delete/{id}")
    public Object delete(@PathVariable("id") String id) {
        log.info("商品删除接口data:{}", id);
        boolean rs = productService.delete(id);
        return Result.create(rs);
    }

    /**
     * 商品查询全部
     *
     * @param pageIndex     页码
     * @param pageSize      页数
     * @param itemId        项目ID
     * @param productTypeId 商品类别
     * @param areaId        商品区域
     * @param positionId    商品位置
     * @return
     */
    @PostMapping(value = "query/list")
    public Object queryList(@RequestBody ProductQryReq req) {
        log.info("商品查询【service】req:{}", req);
        if (commonService.paginationSupport(req.getPageIndex(), req.getPageSize()))
            return productService.queryByPageByJdbc(req);
        List<PaProductDto> result = productService.query(null, null, req.getItemId(),
                req.getProductTypeId(), req.getAreaId(), req.getPositionId(),
                req.getId(), req.getState(), req.getAddTime(), req.getShelfTime(),req.getCommodityType());
        Result<List<PaProductDto>> ret =  Result.create(result);
        log.info("ret:{}",ret);
        return ret;
    }

    /**
     * 商品查询
     *
     * @param id 商品id
     * @return
     */
    @GetMapping(value = "query/{id}")
    public Object query(@PathVariable String id) {
        log.info("商品查询接口id:{}", id);
        List<PaProductDto> rs = productService.query(null, null, null, null,
                null, null, id, null, null, null,null);
        PaProductDto pd = rs.get(0);
        return Result.create(pd);
    }

    /**
     * 商品根据绑定的祝福语编号进行查询
     */
    @GetMapping("queryByid")
    public Object getGreetingsByID(String productId){
        log.info("祝福语根据编号查询列表接口 productId:{}",productId);
        return productService.getGreetingsByID(productId);
    }


}
